import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringCalculator {
	
  public static int add(String data) {
	  
	  int sum = 0;
	  String separator = ",";
	  String separatorString = ",";
	  List<Integer> nums = new ArrayList<Integer>();
	  List<String> lines;
	  boolean illegalArgs = false;
	  String illegalArgsString = "";
	  
	  if (data.equals("")) {
		  return sum;
	  }
	  
	  String[] numLines = data.split("\n");
	  lines = Arrays.asList(numLines);
	  
	  String firstLine = lines.get(0);
	  
	  if (firstLine.startsWith("//")) {
		  if (firstLine.contains("[") && firstLine.contains("]")) {
			  separatorString = firstLine.substring(firstLine.indexOf("[") + 1,firstLine.indexOf("]"));
			  separator = "";
			  for (char c : separatorString.toCharArray()) {
				  separator += ("\\" + c);
			  }
		  } else {
			  separator = "\\" + firstLine.substring(2,3);	
			  separatorString = firstLine.substring(2,3);
		  }
	  }
	  
	  for (String line : lines) {
		  if (line.contains(separatorString)) {
			  for (String num : line.split(separator)) {
				  try {
					  if (Integer.parseInt(num) < 0) {
						  illegalArgs = true;
						  illegalArgsString += num + ", ";						  
					  }
					  else if (!(Integer.parseInt(num) > 1000)) {
						  nums.add(Integer.parseInt(num));
					  }
					  
				  } catch(NumberFormatException e) { } 
			  }
		  } else {
			  nums.add(Integer.parseInt(line));
		  }
	  }
	  
	  if (illegalArgs) {
		  throw new IllegalArgumentException("Negative values not allowed: " + illegalArgsString.substring(0, illegalArgsString.length()-2));
	  }
	  
	  for (int i : nums) {
		  sum += i;
	  }
	  
	  return sum;
  }
}